#!/usr/bin/python3
# -*- coding:utf-8 -*-
from time import sleep

from qturboframe.lib.report.log import logger
from qturboframe.lib.utils.CustomError.custom_error import CustomError
from qturboframe.lib.webdriver import SeleniumError
from qturboframe.public_ui.h_ui.select_tree import SelectTree as HuiSelectTree

from BladeFerm.hui2_app.hui2_lib.tree import Tree


class SelectTree(HuiSelectTree):
    '''
    操作员中心下拉树形框进行封装
    '''


    def set_value_for_multiple(self, value: list = None, timeout=20):
        """
        hui的多选下拉树形框选择

        :Args:
         - value: 选择项的文本集合，例：[XX1/XX2/XX3, XX1/XX4/XX5]
         - timeout: 超时时间，默认是20s

        :Example:
         - 例: select_tree.set_value_for_multiple(['根节点1/子节点1/叶子节点1','根节点2/子节点2/叶子节点2'])
        """
        select_ul_class = r'h-selectTree-dropdown-list'
        # 先全部清空
        self.clear_for_multiple()
        # 弹出下拉框
        selectElement = self._element.find_element_by_attribute(
            'css selector', 'div.h-selectTree-selection', timeout=self.timeout)
        selectElement.secure_click()
        select_div = self._element.exist(
            'css selector', 'div.h-select-dropdown', timeout=0.1)
        if not select_div:
            select_div = self._driver.find_element_by_attribute(
                'xpath', "//body/div[contains(@class,'h-select-dropdown') and not(contains(@style,'display: none'))]",
                timeout=self.timeout)
        # 判断是否存在全部展开
        headButton = select_div.exist('css selector', 'div.h-btn-group', timeout=1)
        if headButton:
            headBtnName = value[0] if value and value[0] in ['全部展开', '全部折叠'] else '全部展开'
            hbt = headButton.find_element_by_attribute(
                'css selector', 'div.h-btn-group>button>span', f'text:{headBtnName}', timeout=1)
            hbt.secure_click()
        timeout1 = 0
        while timeout1 < timeout * 10:
            select_ul = select_div.exist('css selector', 'div.' + select_ul_class, timeout=self.timeout)
            if select_ul.is_displayed():
                break
            timeout1 += 1
            sleep(0.1)
        else:
            raise SeleniumError(CustomError.Error_EQ0301700_Select_Tree_No_Data)
        if len(value) == 1 and value[0] == 'all':
            select_lis = select_ul.find_elements_by_attribute('tag name', 'li', timeout=self.timeout)
            for li in select_lis:
                li.find_element_by_attribute('css selector:', 'span.h-checkbox',
                                             timeout=self.timeout).secure_click()
        else:
            tree = Tree({'css selector': 'ul.h-tree'}, select_div, self.timeout)
            tree.set_value_for_multiple(value)
        selectElement.secure_click()


    def is_default(self, value):
        """
        检查操作员中心下拉树形框默认值
        重写说明：操作员中心下拉树形框默认值有在h-selectTree-input弹框中的，需要兼容到

        :Args:
         - value: 待检查的选项值

        :Returns:
            匹配返回True，否则返回False

        :Example:
         - 例1: select_tree.is_default('节点1,节点2')
         - 例2: select_tree.is_default('节点1')
        """
        select_div = self._element.find_element_by_attribute(
            'css selector', 'div.h-selectTree-selection', timeout=self.timeout)
        title = select_div.get_attribute('title')
        if title:
            if value == title:
                return True
            else:
                logger.error('默认值不匹配！')
                return False
        span_default = select_div.exist(
            'css selector', 'span.h-selectTree-placeholder', timeout=self.timeout)
        if span_default and span_default.is_displayed():
            text = span_default.text
            if value == text:
                return True
            else:
                logger.error('默认值不匹配！')
                return False
        input_default = select_div.exist('css selector', 'input.h-selectTree-input', timeout=self.timeout)
        if input_default:
            if input_default.text:
                text = input_default.text
            elif input_default.get_attribute('value'):
                text = input_default.get_attribute('value')
            elif input_default.get_attribute('placeholder'):
                text = input_default.get_attribute('placeholder')
            elif input_default.get_attribute('title'):
                text = input_default.get_attribute('title')
            else:
                text = ''
            if value == text:
                return True
            else:
                logger.error('默认值不匹配！')
                return False
        else:
            logger.error('没有默认值！')
            return False